using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class OutContactUDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        public List<OutContactUs> GetOutContactUList()
        {
            try
            {
                const string spName = "spo_OutContactUs_GetList";

                var OutContactUList = DbHelper.ExecuteSPList<OutContactUs>(spName);
                return OutContactUList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return new List<OutContactUs>();
            }
        }

        public List<OutContactUs> GetOutContactUListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "spo_OutContactUs_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var OutContactUList = DbHelper.ExecuteSqlList<OutContactUs>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return OutContactUList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return new List<OutContactUs>();
            }
        }

        public OutContactUs GetOutContactUById(int contactId)
        {
            try
            {
                const string spName = "spo_OutContactUs_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@ContactId", contactId)
                                    };

                var OutContactU = DbHelper.ExecuteSPList<OutContactUs>(spName, sqlParams).SingleOrDefault();
                return OutContactU;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return null;
            }
        }

        public int InsertOutContactU(OutContactUs OutContactU)
        {
            try
            {
                const string spName = "spo_OutContactUs_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@ContactName", OutContactU.ContactName),
										new SqlParameter("@ContactMail", OutContactU.ContactMail),
										new SqlParameter("@ContactContent", OutContactU.ContactContent),
										new SqlParameter("@ContactStt", OutContactU.ContactStt),
										new SqlParameter("@ContactDate", OutContactU.ContactDate),
										new SqlParameter("@ContactId", OutContactU.ContactId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@ContactId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ContactId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@ContactId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int UpdateOutContactU(OutContactUs OutContactU)
        {
            try
            {
                const string spName = "spo_OutContactUs_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@ContactId", OutContactU.ContactId)
										, new SqlParameter("@ContactName", OutContactU.ContactName)
										, new SqlParameter("@ContactMail", OutContactU.ContactMail)
										, new SqlParameter("@ContactContent", OutContactU.ContactContent)
										, new SqlParameter("@ContactStt", OutContactU.ContactStt)
										, new SqlParameter("@ContactDate", OutContactU.ContactDate)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int DeleteOutContactU(int contactId)
        {
            try
            {
                const string spName = "spo_OutContactUs_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@ContactId", contactId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
